﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Exp.BillDLL.biz;
using Exp.BillDLL.ety;

namespace ExpBillMain.form
{
    public partial class frmInvoiceHdr : Dev.Control.BaseFormDev
    {
        bizInvoiceHdr bzInvoiceHdr;

        public frmInvoiceHdr()
        {
            InitializeComponent();
        }

        private void frmInvoiceHdr_Load(object sender, EventArgs e)
        {
            bzInvoiceHdr = new bizInvoiceHdr(_Global.basSysPara);

            string sMapingCols;
            string sMapingHeaderTxt;
            string sColumnWidth;
            //客户下拉
            sMapingCols = "CLIENT_NAME";
            sMapingHeaderTxt = "客户名称";
            sColumnWidth = "20";
            cboClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboClient.DisplayMember = "CLIENT_NAME";
            cboClient.ValueMember = "CLIENT_ID";
            cboClient.DataSource = _Global.ilClient;

            ggcDocList.GridView = this.gridView1;
            sMapingCols = "YID,YD_NO,CLIENT_NAME,YW_TYPE_DESC,SF_DESC,CITY_DESC,QX_DESC,YF_AMT,EXP_AMOUNT,CREATE_TIME,QTY,WGT,ELE_CONFIRM";
            sMapingHeaderTxt = "序号,详情单号,客户名称,业务类型,省份,市,区,速递金额,计费金额,时间,数量,重量,电子客户确认";
            sColumnWidth = string.Empty;
            ggcDocList.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcDocList);

            cboClient.Text = "康爱多";
            dtFrom.Value = new DateTime(2014, 1, 1);
        }

        public override Dev.Control.etyQuickKey F9()
        {
            etyTmYmsYD eyEty = new etyTmYmsYD();
            eyEty.FROM_DATE = dtFrom.Value.ToString("yyyyMMdd");
            eyEty.TO_DATE = dtTo.Value.AddDays(1).ToString("yyyyMMdd");
            if (cboClient.Text.Trim() != "")
            {
                eyEty.CLIENT_ID = cboClient.EditValue.ToString();
            }
            IList<etyTmYmsYD> ilYmsYD = new List<etyTmYmsYD>();
            ilYmsYD = bzInvoiceHdr.GetDtlPgBill(eyEty);
            srcBinding.DataSource = ilYmsYD;
            ggcDocList.DataSource = srcBinding;

            //统计
            IEnumerable<IGrouping<string, etyTmYmsYD>> iGrp = ilYmsYD.GroupBy(pallet => pallet.CLIENT_ID);
            foreach (IGrouping<string, etyTmYmsYD> grp in iGrp)
            {
                txtSumCount.Text = grp.Max(p => p.YD_NO).ToString();
                txtSumCount.Text = grp.Count().ToString();
                txtSumWgt.Text = grp.Sum(p => decimal.Parse(p.WGT)).ToString();
                txtSumAmount.Text = grp.Sum(p =>decimal.Parse(p.EXP_AMOUNT)).ToString();
            }
            return base.F9();
        }
    }
}
